package com.saint.swipemenulistview.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.saint.swipemenulistview.R;
import com.saint.swipemenulistview.runtimepermissions.PermissionsManager;
import com.saint.swipemenulistview.runtimepermissions.PermissionsResultAction;

import org.xutils.view.annotation.ViewInject;

/**
 * Created by Administrator on 2017/11/9.
 */

public class EasemobActivity extends BaseActivity {
    public static final String TAG = "EasemobActivity";


    @ViewInject(R.id.ease_mob_tv)
    private TextView dataTV;
    @ViewInject(R.id.user_name_et)
    private EditText userName_et;
    @ViewInject(R.id.pwd_et)
    private EditText pwd_et;


    private String userName, pwd;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected int setContent() {
        return R.layout.act_easemob;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDate() {
        //注册失败会抛出HyphenateException
//        try {
//            EMClient.getInstance().createAccount("a18137173079", "a18137173079");//同步方法
//        } catch (HyphenateException e) {
//            e.printStackTrace();
//            Toast.makeText(act, "环信注册失败code: " + e.getErrorCode() + "  msg: " + e.getMessage(), Toast.LENGTH_SHORT).show();
//            Log.e(TAG, "环信注册失败code: " + e.getErrorCode() + "  msg: " + e.getMessage());
//        }

        getPermissions();


    }

    @Override
    protected void startAct(Activity startAct) {
        intent = new Intent(startAct, EasemobActivity.class);
        startAct.startActivity(intent);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
//                if (EMClient.getInstance().isLoggedInBefore()) {
//                    if (EMClient.getInstance().getCurrentUser().equals(userName)) {
//
//                        Toast.makeText(act, "已登录过", Toast.LENGTH_SHORT).show();
//                        Log.e(TAG, "EMClient.getInstance().getCurrentUser()" + EMClient.getInstance().getCurrentUser());
//                        EMClient.getInstance().chatManager().loadAllConversations();
//                        EMClient.getInstance().groupManager().loadAllGroups();
//                    } else {
//                        getLoginData();
//                    }
//                } else {
                    getLoginData();
//                }

                break;
            case R.id.get_wechat_btn:
                break;
            case R.id.logout_btn:

                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void getLoginData() {
        userName = userName_et.getText().toString().trim();
        pwd = pwd_et.getText().toString().trim();
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(act, "用户名密码不得为空", Toast.LENGTH_SHORT).show();
            return;
        }
    }




    private void getPermissions() {
        /**
         * 请求所有必要的权限----原理就是获取清单文件中申请的权限
         */
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                Toast.makeText(act, "All permissions have been granted", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(String permission) {
                Toast.makeText(act, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
